package com.mao.server.utils;

import com.mao.server.constant.JwtClaimsConstant;
import com.mao.server.domain.po.User;
import com.mao.server.properties.JwtProperties;
import io.jsonwebtoken.Claims;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.compress.archivers.sevenz.CLI;

import java.util.HashMap;
import java.util.Map;

@Slf4j
@RequiredArgsConstructor
public class TokenUtil {
    final private static JwtProperties jwtProperties = new JwtProperties();

    public static String createToken(User user) {
        Map<String, Object> claims = new HashMap<>();
        claims.put(JwtClaimsConstant.USER_ID, user.getId());
        String token = JwtUtil.createJWT(jwtProperties.getUserSecretKey(), jwtProperties.getUserTtl(), claims);
        return token;
    }

    public static void verifyToken(String token) {
        Claims claims = JwtUtil.parseJWT(jwtProperties.getUserSecretKey(), token);
        Long userId = Long.valueOf(claims.get(JwtClaimsConstant.USER_ID).toString());
        BaseContext.setCurrentId(userId);
    }
}
